package com.lzk.user.controller;

import com.lzk.basic.excption.BusinessException;
import com.lzk.basic.util.JsonResult;
import com.lzk.basic.util.PageList;
import com.lzk.user.domain.User;
import com.lzk.user.dto.UserDto;
import com.lzk.user.query.UserQuery;
import com.lzk.user.serice.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("/user")

@Api(value = "用户的API",description="用户相关的CRUD功能")
public class UserController {

    @Resource
    private IUserService userService;


    /**
     * 用户注册
     * @param userDto
     * @return
     */
    @PostMapping("/register/phone")
    public JsonResult register(@RequestBody UserDto userDto){
        try {
            userService.phoneRegister(userDto);
            return JsonResult.me();
        }
        catch (BusinessException e){
            e.printStackTrace();
            return JsonResult.me(false,"注册失败!"+e.getMessage());
        } catch (Exception e){
            e.printStackTrace();
            return JsonResult.me(false,"系统错误!"+e.getMessage());
        }

    }





    /**
     * 查询所有数据
     * @return
     */
    @ApiOperation(value = "查询所有数据" )
    @GetMapping
    public List<User> findAll(){
        return userService.loadAll();
    }

    /**
     * 查询一个 接收参数id
     * @param id
     * @return
     */
    @ApiOperation(value = "查询一个数据" )
    @GetMapping("/{id}")
    public User findOne(@PathVariable("id") Long id){
        return userService.loadById(id);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @ApiOperation(value = "删除一个数据" )
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            userService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  JsonResult.me(false,"操作失败");
        }
    }

    /**
     * 添加或修改
     * @param user
     * @return
     */
    @ApiOperation(value = "添加或修改数据" )
    @PutMapping
    public JsonResult updateOrAdd(@RequestBody User user){
        try {
            if (user.getId() == null){
                userService.save(user);
            }else {
                userService.update(user);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  JsonResult.me(false,"操作失败");
        }
    }
    /**
     * 分页查询或高级查询
     * @param userQuery
     * @return
     */
    @ApiOperation(value = "分页查询或高级查询" )
    @PostMapping
    public PageList<User> queryPage(@RequestBody UserQuery userQuery){
        return userService.queryData(userQuery);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除一个数据" )
    @PatchMapping
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
            userService.patchRemove(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return  JsonResult.me(false,"操作失败");
        }
    }
}
